package Thread.BlockingQueue;

import java.sql.Timestamp;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}22:45
 */
public class BlockingQueueDemo {
    public static void main(String[] args) {
//        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);//只有三个位置
//        System.out.println(blockingQueue.add("a"));
//        System.out.println(blockingQueue.add("b"));
//        System.out.println(blockingQueue.add("c"));

        BlockingQueue<String> blockingQueue = new SynchronousQueue<>();

        new Thread(() -> {
            try {
                System.out.println(Thread.currentThread().getName()+"生成");
                blockingQueue.put("1");
                System.out.println(Thread.currentThread().getName()+"生成");
                blockingQueue.put("2");
                System.out.println(Thread.currentThread().getName()+"生成");
                blockingQueue.put("3");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"A").start();

        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
                System.out.println(blockingQueue.take());
                TimeUnit.SECONDS.sleep(1);
                System.out.println(blockingQueue.take());
                TimeUnit.SECONDS.sleep(1);
                System.out.println(blockingQueue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"B").start();

    }
}
